package com.malty.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.malty.mall.dao.pms.PmsBrandMapper;
import com.malty.mall.domain.dto.query.BrandQuery;
import com.malty.mall.domain.dto.req.pms.PmsBrandParam;
import com.malty.mall.domain.dto.req.UpdateStatusReq;
import com.malty.mall.domain.entity.pms.PmsBrand;
import com.malty.mall.service.pms.IPmsBrandService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * @author malty
 */
@Service
public class PmsBrandServiceImpl extends ServiceImpl<PmsBrandMapper, PmsBrand> implements IPmsBrandService {
    @Override
    public Boolean create(PmsBrandParam brandParam) {
        PmsBrand pmsBrand = new PmsBrand(brandParam);
        return this.save(pmsBrand);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateById(PmsBrandParam brandParam) {
        PmsBrand pmsBrand = new PmsBrand(brandParam);
        // todo 更新品牌时 要更新 商品中的品牌名称
        return this.updateById(pmsBrand);
    }

    @Override
    public Boolean updateShowStatus(UpdateStatusReq updateStatusReq) {
        PmsBrand pmsBrand = new PmsBrand();
        pmsBrand.setShowStatus(updateStatusReq.getStatus());
        return this.update(pmsBrand, Wrappers.<PmsBrand>lambdaQuery().in(PmsBrand::getId, updateStatusReq.getIds()));
    }

    @Override
    public Boolean updateFactoryStatus(UpdateStatusReq statusReq) {
        PmsBrand pmsBrand = new PmsBrand();
        pmsBrand.setFactoryStatus(statusReq.getStatus());
        return this.update(pmsBrand, Wrappers.<PmsBrand>lambdaQuery().in(PmsBrand::getId, statusReq.getIds()));
    }

    @Override
    public PmsBrandParam getBrandById(Integer id) {
        PmsBrand pmsBrand = this.getById(id);
        return new PmsBrandParam(pmsBrand);
    }

    @Override
    public List<PmsBrandParam> listAll() {
        List<PmsBrand> pmsBrands = this.list();
        return PmsBrandParam.brandParams(pmsBrands);
    }

    @Override
    public List<PmsBrandParam> listBrands(Integer pageNum, Integer pageSize, BrandQuery brandQuery) {
        Page<PmsBrand> brandPage = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<PmsBrand> wrapper = buildWrappers(brandQuery);
        Page<PmsBrand> page = this.page(brandPage, wrapper);
        List<PmsBrand> pmsBrands = page.getRecords();
        return PmsBrandParam.brandParams(pmsBrands);
    }

    private LambdaQueryWrapper<PmsBrand> buildWrappers(BrandQuery brandQuery) {
        LambdaQueryWrapper<PmsBrand> wrapper = Wrappers.lambdaQuery();
        if (!ObjectUtils.isEmpty(brandQuery.getName())) {
            wrapper.like(PmsBrand::getName, brandQuery.getName());
        }
        return wrapper;
    }

    @Override
    public Boolean deleteById(Integer id) {
        return this.removeById(id);
    }
}
